//
//  GPSTrainingMapModel.swift
//  DaRings
//
//  Created by 尹琼 on 2023/2/2.
//

import UIKit
import MapKit

open class GPSTrainingMapModel: NSObject {
    
    public var totlaDistance: CLLocationDistance = 0
    
    public var startPoint = MKPointAnnotation()
    public var endPoint = MKPointAnnotation()
    
    public var coordinateModels: [GPSTrainingCoordinateModel] = []
    
    // 总坐标系
    open var coordiantes: [CLLocationCoordinate2D] {
        
        return coordinateModels.map { $0.coordiante }
    }
    
    open var colors: [UIColor] {
        return coordinateModels.map { $0.speedRangeType.color }
    }
    

    public init(gpsTrainingModel: GPSTrainingModel) {
        super.init()
        
        totlaDistance = gpsTrainingModel.totalDistance()
        
        coordinatesAndPolylines(gpsTrainingModel: gpsTrainingModel)
        
    }
    
    /// 彩色轨迹路径计算
    open func coordinatesAndPolylines(gpsTrainingModel: GPSTrainingModel) {
        
        let locations = gpsTrainingModel.locations
        
        var coordinateModels: [GPSTrainingCoordinateModel] = []
        
        if locations.count < 2 {
            return
        }
        
        for (i, location) in locations.enumerated() {
            
            let speed = 1000/60 / location.speed
            
            
            let range = GPSTrainingSpeedRangeType.rangeType(with: speed)
           
            let loc = CLLocation.initWith(trainingLocation: location)
            
            let coordinate = loc.coordinate

            if i == 0 {
                
                startPoint.coordinate = coordinate
                startPoint.title = "Start"
                
            } else if i == locations.count - 1 {
                
                endPoint.coordinate = coordinate
                endPoint.title = "End"
                
            }
            
            let coordianteModel = GPSTrainingCoordinateModel(coordiante: coordinate, speedRangeType: range)
            coordinateModels.append(coordianteModel)

        }

        self.coordinateModels = coordinateModels
        
      
        
    }
    
    
}


public struct GPSTrainingCoordinateModel {
    
    /// 这一段坐标
    public var coordiante: CLLocationCoordinate2D
  
    /// 速度区间类型
    public var speedRangeType: GPSTrainingSpeedRangeType
    
    
}


